package com.haochang.chunkvod.app.database.order;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.VodApplication;
import com.haochang.chunkvod.model.order.PartInfo;
import com.haochang.chunkvod.view.vod.spinner.SpinnerInfo;
import com.j256.ormlite.dao.RuntimeExceptionDao;

import android.content.Context;
import android.database.Cursor;

/**
 *@author qintao
 *created at 2016/6/15 17:37
 */

public class PartInfoDao extends OrderDaoManager<PartInfo>
{
	private static PartInfoDao dao;
	private final static Object gLock = new Object();

	private PartInfoDao(Context context)
	{
		super(context);

	}

	public static PartInfoDao getDao()
	{
		if (dao == null)
			dao = new PartInfoDao(VodApplication.appContext);
		return dao;
	}

	/**
	 * 查询分区信息
	 *
	 * @return
	 */
	public List<SpinnerInfo> queryPartInfo()
	{
		synchronized (gLock)
		{
			List<SpinnerInfo> resultList = new ArrayList<SpinnerInfo>();
			if (openHelper())
			{
				RuntimeExceptionDao<PartInfo, Integer> dao = this.openHelper.getRuntimeExceptionDao(PartInfo.class);
				if (dao != null)
				{
					StringBuffer strBuffer = new StringBuffer();
					strBuffer.append(" select part_id,part_name from tb_part");
					Cursor cur = this.openHelper.getWritableDatabase().rawQuery(strBuffer.toString(), null);
					if (cur != null)
					{
						if (cur.getCount() > 0)
						{
							while (cur.moveToNext())
							{
								SpinnerInfo info = new SpinnerInfo();
								info.setCode(cur.getString(0));
								info.setName(cur.getString(1));
								resultList.add(info);
							}
						}
						if (!cur.isClosed())
							cur.close();
					}
				}
				closeHelper();
			}
			return resultList;
		}
	}

}
